package cn.gmph.blog.service.sys.dao.helper;

import org.apache.ibatis.jdbc.SQL;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.gmph.core.utils.StringUtils;

import cn.gmph.blog.service.sys.entity.LinksDO;
public class LinksProvider {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String TABLE_NAME = "wp_links";

    public String update(Map<String, Object> params) {
        LinksDO links = (LinksDO) params.get("links");
        return new SQL() {{
        UPDATE(TABLE_NAME);
        if (links.getLinkId() != null) {
            SET("link_id=#{links.linkId}");
        }
        if (StringUtils.isNotBlank(links.getLinkUrl())){
            SET("link_url=#{links.linkUrl}");
        }
        if (StringUtils.isNotBlank(links.getLinkName())){
            SET("link_name=#{links.linkName}");
        }
        if (StringUtils.isNotBlank(links.getLinkImage())){
            SET("link_image=#{links.linkImage}");
        }
        if (StringUtils.isNotBlank(links.getLinkTarget())){
            SET("link_target=#{links.linkTarget}");
        }
        if (StringUtils.isNotBlank(links.getLinkDescription())){
            SET("link_description=#{links.linkDescription}");
        }
        if (StringUtils.isNotBlank(links.getLinkVisible())){
            SET("link_visible=#{links.linkVisible}");
        }
        if (links.getLinkOwner() != null) {
            SET("link_owner=#{links.linkOwner}");
        }
        if (links.getLinkRating() != null) {
            SET("link_rating=#{links.linkRating}");
        }
        if (links.getLinkUpdated() != null) {
            SET("link_updated=#{links.linkUpdated}");
        }
        if (StringUtils.isNotBlank(links.getLinkRel())){
            SET("link_rel=#{links.linkRel}");
        }
        if (StringUtils.isNotBlank(links.getLinkNotes())){
            SET("link_notes=#{links.linkNotes}");
        }
        if (StringUtils.isNotBlank(links.getLinkRss())){
            SET("link_rss=#{links.linkRss}");
        }
        WHERE("id = #{links.id}");
        }}.toString();
    }

    public String pageList(Map<String, Object> params) {
        LinksDO links = (LinksDO) params.get("links");
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {{
        SELECT("*");
        FROM(TABLE_NAME);
        if (links.getLinkId() != null) {
            WHERE("link_id=#{links.linkId}");
        }
        if (StringUtils.isNotBlank(links.getLinkUrl())){
            WHERE("link_url=#{links.linkUrl}");
        }
        if (StringUtils.isNotBlank(links.getLinkName())){
            WHERE("link_name=#{links.linkName}");
        }
        if (StringUtils.isNotBlank(links.getLinkImage())){
            WHERE("link_image=#{links.linkImage}");
        }
        if (StringUtils.isNotBlank(links.getLinkTarget())){
            WHERE("link_target=#{links.linkTarget}");
        }
        if (StringUtils.isNotBlank(links.getLinkDescription())){
            WHERE("link_description=#{links.linkDescription}");
        }
        if (StringUtils.isNotBlank(links.getLinkVisible())){
            WHERE("link_visible=#{links.linkVisible}");
        }
        if (links.getLinkOwner() != null) {
            WHERE("link_owner=#{links.linkOwner}");
        }
        if (links.getLinkRating() != null) {
            WHERE("link_rating=#{links.linkRating}");
        }
        if (links.getLinkUpdated() != null) {
            WHERE("link_updated=#{links.linkUpdated}");
        }
        if (StringUtils.isNotBlank(links.getLinkRel())){
            WHERE("link_rel=#{links.linkRel}");
        }
        if (StringUtils.isNotBlank(links.getLinkNotes())){
            WHERE("link_notes=#{links.linkNotes}");
        }
        if (StringUtils.isNotBlank(links.getLinkRss())){
            WHERE("link_rss=#{links.linkRss}");
        }
        ORDER_BY("id desc limit " + start + ", " + limit );
        }}.toString();
    }

    public String pageListCount(Map<String, Object> params) {
        LinksDO links = (LinksDO) params.get("links");
        return new SQL() {{
        SELECT("count(1)");
        FROM(TABLE_NAME);
        if (links.getLinkId() != null) {
            WHERE("link_id=#{links.linkId}");
        }
        if (StringUtils.isNotBlank(links.getLinkUrl())){
            WHERE("link_url=#{links.linkUrl}");
        }
        if (StringUtils.isNotBlank(links.getLinkName())){
            WHERE("link_name=#{links.linkName}");
        }
        if (StringUtils.isNotBlank(links.getLinkImage())){
            WHERE("link_image=#{links.linkImage}");
        }
        if (StringUtils.isNotBlank(links.getLinkTarget())){
            WHERE("link_target=#{links.linkTarget}");
        }
        if (StringUtils.isNotBlank(links.getLinkDescription())){
            WHERE("link_description=#{links.linkDescription}");
        }
        if (StringUtils.isNotBlank(links.getLinkVisible())){
            WHERE("link_visible=#{links.linkVisible}");
        }
        if (links.getLinkOwner() != null) {
            WHERE("link_owner=#{links.linkOwner}");
        }
        if (links.getLinkRating() != null) {
            WHERE("link_rating=#{links.linkRating}");
        }
        if (links.getLinkUpdated() != null) {
            WHERE("link_updated=#{links.linkUpdated}");
        }
        if (StringUtils.isNotBlank(links.getLinkRel())){
            WHERE("link_rel=#{links.linkRel}");
        }
        if (StringUtils.isNotBlank(links.getLinkNotes())){
            WHERE("link_notes=#{links.linkNotes}");
        }
        if (StringUtils.isNotBlank(links.getLinkRss())){
            WHERE("link_rss=#{links.linkRss}");
        }
        }}.toString();
    }
}

